package com.example.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.dao.FileNodeRepository;
import com.example.dao.KnowledgeElementRepository;
import com.example.entity.FileNode;
import com.example.entity.KnowledgeElement;
import com.example.service.FileNodeService;
import com.example.service.FileService;
import com.example.service.KnowledgeElementService;
import com.example.utils.ConstantVariable;
import com.example.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import java.util.*;

/**
 *
 */
@Service
public class KnowledgeElementServiceImpl implements KnowledgeElementService {
    @Autowired
    KnowledgeElementRepository knowledgeElementRepository;
    @Autowired
    FileService fileService;
    @Autowired
    FileNodeService fileNodeService;
    @Autowired
    FileNodeRepository fileNodeRepository;
    @Override
    public KnowledgeElement selectById(Long id){
        Optional<KnowledgeElement> knowledgeElement_Optional = knowledgeElementRepository.findById(id);
        KnowledgeElement knowledgeElementNode = knowledgeElement_Optional.get();
//        List<FileNode> fileNodeList = knowledgeElementNode.getFileNodeList();
//        for (FileNode fileNode:fileNodeList) {
//            if (fileNode.getScene().equals("image")) {
//                knowledgeElementNode.getImageIdList().add(fileNode.getId());
//            }else if (fileNode.getScene().equals("pdf")) {
//                knowledgeElementNode.setPdfId(fileNode.getId());
//            }
//        }
        return knowledgeElementNode;
    }

    @Override
    public KnowledgeElement updateContent(KnowledgeElement knowledgeElement) {
        Optional<KnowledgeElement> knowledgeElement_Optional = knowledgeElementRepository.findById(knowledgeElement.getId());
        KnowledgeElement knowledgeElementNode = knowledgeElement_Optional.get();
        knowledgeElementNode.setContent(knowledgeElement.getContent());
        knowledgeElementNode = knowledgeElementRepository.save(knowledgeElementNode);
        return knowledgeElementNode;
    }







    @Override
    public JSONObject deleteById(Long id) {
        JSONObject jsonObject = new JSONObject();
        Optional<KnowledgeElement> knowledgeElement_Optional = knowledgeElementRepository.findById(id);
        KnowledgeElement knowledgeElementNode = knowledgeElement_Optional.get();
        if (knowledgeElementNode.getFileNodeList().size() == 0) {
            knowledgeElementRepository.deleteById(id);
            jsonObject.put("msg", "success");
            jsonObject.put("data", knowledgeElementNode);
            return jsonObject;
        }
        jsonObject.put("msg", "删除失败，因为还有连接关系");
        return jsonObject;
    }

    @Override
    public JSONObject update(KnowledgeElement knowledgeElement) {
        JSONObject ans = new JSONObject();
        List<Long> imageIdList = knowledgeElement.getImageIdList();
        Long pdfId = knowledgeElement.getPdfId();
        Long keId = knowledgeElement.getId();
        Optional<KnowledgeElement> knowledgeElementOptional = knowledgeElementRepository.findById(keId);
        KnowledgeElement knowledgeElementNode = knowledgeElementOptional.get();
        knowledgeElementNode.getFileNodeList().clear();
        knowledgeElementNode.getImageIdList().clear();
        knowledgeElementNode.setPdfId(null);
        boolean curHasChild = false;
        for (Long imageNodeId:imageIdList) {
            JSONObject jsonObject_image = fileNodeService.selectOneById(imageNodeId);
            FileNode imageNode = (FileNode)jsonObject_image.get("data");
            imageNode.setHasParent(true);
            curHasChild = true;
            imageNode = fileNodeRepository.save(imageNode);
            knowledgeElementNode.getFileNodeList().add(imageNode);
            knowledgeElementNode.getImageIdList().add(imageNodeId);
        }
        if (pdfId != null) {
            JSONObject jsonObject_pdf = fileNodeService.selectOneById(pdfId);
            FileNode pdfNode = (FileNode)jsonObject_pdf.get("data");
            pdfNode.setHasParent(true);
            curHasChild = true;
            pdfNode = fileNodeRepository.save(pdfNode);
            knowledgeElementNode.getFileNodeList().add(pdfNode);
            knowledgeElementNode.setPdfId(pdfId);
        }
        knowledgeElementNode.setHasChild(curHasChild);
        knowledgeElementNode.setContent(knowledgeElement.getContent());
        knowledgeElementNode = knowledgeElementRepository.save(knowledgeElementNode);
        ans.put("msg", "success");
        ans.put("data", knowledgeElementNode);
        return ans;
    }

    @Override
    public JSONObject selectAll() {
        JSONObject jsonObject = new JSONObject();
        List<KnowledgeElement> knowledgeElementList = knowledgeElementRepository.findAll();
        jsonObject.put("msg", "success");
        jsonObject.put("data", knowledgeElementList);
        return jsonObject;
    }

    @Override
    public JSONObject insert(KnowledgeElement knowledgeElement) {
        JSONObject ans = new JSONObject();

        List<Long> imageIdList = knowledgeElement.getImageIdList();
        Long pdfId = knowledgeElement.getPdfId();
        boolean curHasChild = false;
        for (Long imageId:imageIdList) {
            JSONObject jsonObject_image = fileNodeService.selectOneById(imageId);
            FileNode imageFileNode = (FileNode)jsonObject_image.get("data");
            imageFileNode.setHasParent(true);
            curHasChild = true;
            imageFileNode = fileNodeRepository.save(imageFileNode);
            knowledgeElement.getFileNodeList().add(imageFileNode);
        }
        if (pdfId != null) {
            JSONObject jsonObject_pdf = fileNodeService.selectOneById(pdfId);
            FileNode pdfFileNode = (FileNode)jsonObject_pdf.get("data");
            pdfFileNode.setHasParent(true);
            curHasChild = true;
            pdfFileNode = fileNodeRepository.save(pdfFileNode);
            knowledgeElement.getFileNodeList().add(pdfFileNode);
        }

        String uuid = UUID.randomUUID().toString();
        knowledgeElement.setLogic_id(uuid);
        knowledgeElement.setHasChild(curHasChild);
        KnowledgeElement knowledgeElementNode = knowledgeElementRepository.save(knowledgeElement);
        ans.put("msg", "success");
        ans.put("data", knowledgeElementNode);
        return ans;
    }

}
